/**
 * 项目名称: Topic_572.cpp
 * 项目路径: study/DataStructure/LeetCode/树/简单
 * 编辑时间: 2024年08月10日08时42分19秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int lNull = 100010,rNull = 100011;
    vector<int>lT,rT;
    void pre(TreeNode * root,vector<int> &t){
        if(!root)return;
        t.push_back(root->val);
        if(root->left)pre(root->left,t);
        else t.push_back(lNull);
        if(root->right)pre(root->right,t);
        else t.push_back(rNull);
    }
    bool kmp(){
        int ne[10010];
        int sLen = lT.size(), tLen = rT.size();
        vector <int> fail(rT.size(), -1);

        for(int i=1,j=-1;i<rT.size();i++){
            while (j != -1&& rT[i]!=rT[j+1])j=fail[j];
            if(rT[i]==rT[j+1])j++;
            fail[i]=j;
        }

        for(int i =0,j=-1;i<lT.size();i++){
            while (j != -1&&lT[i]!=rT[j+1])j=fail[j];
            if(lT[i] == rT[j+1])j++;
            if(j==rT.size()-1)return true;
        }
        return false;
    }
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        pre(root,lT);
        pre(subRoot,rT);
        for(auto x:lT)cout<<x<<" ";
        cout<<endl;
        for(auto x:rT)cout<<x<<" ";

        return kmp();
    }
};

int main() {

    return 0;
}
